<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class PhotoController extends Controller
{
    static public function getPho(){
    $data = DB::table('cate') ->select(DB::raw("*,concat(path,',',id) as paths")) -> orderBy('paths')-> get();
    return $data;

    }

    public function getAdd(){
    	//添加
    	
    	//dd($data);
        return view('admin/photo/add',['data'=>self::getPho()]);
    }

    //执行添加
     public function postInsert(Request $request){	
     	 $arr = $request->only(['title','cid','count','cate','url']);
     	 //dd($arr); 
     	 $arr['createtime'] = date('Y-m-d H:i:s',time()); 
     	 //dd($request -> hasFile('pic'));
     	 
     	 if($request -> hasFile('pic')){
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('pic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('pic') -> move('./upload/photo',$tname);
        	$arr['pic'] = $tname; 
        }

    	$res = DB::table('photo') -> insert($arr);
	    if($res){
	            return redirect('/admin/photo/index') -> with('success','添加成功');
	       }else{
	            return back() -> with('error','添加失败');
	       }     
	    
    }

    //列表
    public function getIndex(Request $request){
    	 // 分页
        $count = $request -> input('count',10);
       	// 关键字
        $search = $request -> input('search','');
        // 对查询语句构造器进行分页  paginate  查询数据并且分页
        $data = DB::table('photo') ->where('title','like','%'.$search.'%')-> paginate($count);
        return view('admin/photo/index ',['data'=>$data,'count'=>$count,'search'=>$search,'request'=>$request->all()]);
    }

    //删除
    public function getDelete($id){
    	//echo 11111;
    	 $data = DB::table('photo') -> where('id',$id) -> delete();
    	 if($data = 1){
         return redirect('/admin/photo/index') -> with('success','删除成功');
      }else{
        return back() -> with('error','删除失败');
    }

  }

  //修改


  	public function getEdit($id){
  		$datas = DB::table('photo') -> where('id',$id) -> first();
  		//dd($datas);
      $cates = array(1=>"首页",2=>"产品",3=>"商城",4=>"公告",5=>"服务");                                                                                                                                                 
      //dd($cates);
  		return view('admin/photo/edit',['datas'=>$datas,'data'=>self::getPho(),'cates'=>$cates]);
  	}

 	//修改
 	public function postUpdate(Request $request){
 		//echo 11222;
 		 
 		 $id  = $request->input('id');
 		 $cid  = $request->input('cid');
 		 $title  = $request->input('title');
 		 $count  = $request->input('count');
 		 $status  = $request->input('status');
     $url = $request->input('url');
 		//判断是否修改头像
        if($request -> hasFile('pic')){
            //随机文件名
            $tem_name = md5(time().rand(10000,99999));
            //获取上传文件的后缀名
            $hou_name = $request->file('pic')->getClientOriginalExtension();
            //拼接
            $pic = $tem_name.'.'.$hou_name;
            //移动到指定的文件夹
             $request -> file('pic') -> move('./upload/photo',$pic);
            //dd($pic);
        }else{
            $pic = $request->input('oldpic');
            //dd($pic);
        }
        if($request -> input('cate') > 0){
          $cate = $request->input('cate');
        }else{
          $cate = $request->input('ocate');
        }

        //dd($cate);
         //将值更新到数据库中
       $data = DB::table('photo')->where('id',$id)->update(['cid'=>$cid,'title'=>$title,'count'=>$count,'status'=>$status,'pic'=>$pic,'cate'=>$cate,'url'=>$url]); 

       if($data = 1){
        return redirect('/admin/photo/index') -> with('success','修改成功');
        }else{
       return back() -> with('error','修改失败');
        } 

 	}

    	
}
